
import 'package:flutter/material.dart';
import 'package:flutterlib/common/constants/constants.dart';
import 'package:flutterlib/common/net/dao_result.dart';
import 'package:flutterlib/common/net/retrofit/post_api.dart';
import 'package:flutterlib/common/style/quhui_style.dart';
import 'package:flutterlib/common/utils/navigator_utils.dart';
import 'package:flutterlib/common/utils/toast.dart';
import 'package:flutterlib/model/post/post_bean.dart';
import 'package:flutterlib/pages/post/post_list_widget_page.dart';
import 'package:flutterlib/widgets/bussiness/post/post_list_item.dart';
import 'package:flutterlib/widgets/bussiness/post/post_simple_list_item.dart';
import 'package:flutterlib/widgets/dialog/heart_dialog.dart';

class PostNormalListWidgetPage extends PostListWidgetPage {
  final String name;
  final int interestId;
  final int type;

  PostNormalListWidgetPage({this.name, this.interestId, this.type, Key key})
      : super(key: key);

  @override
  PostNormalListWidgetPageState createState() => PostNormalListWidgetPageState();
}

///页面 KeepAlive ，同时支持 动画Ticker
class PostNormalListWidgetPageState extends PostListWidgetPageState<PostNormalListWidgetPage>
    {

  getDataLogic(int page) async {
    print(page);

    if(widget.interestId == 0){
      var posts = await PostApi.builder().pageByQuery(
          page - 1, 20, "createTime,desc", 0, 0, "PUBLISHED");
      return new DataResult(posts.content, true);
    }else{
      var posts = await PostApi.builder().pageByInterestQuery(
          page - 1, 20, "createTime,desc", widget.interestId);
      return new DataResult(posts.content, true);
    }
  }

  Widget renderItem(index, post) {
    //提交
    // var post = pullLoadWidgetControl.dataList[index];
    return
         PostListItem(post, onPressed: () {
      NavigatorUtils.goPostDetail(context, post.id, name : widget.name);
    }, onZhanPressed: (isLike)async{
      if(!isLogin){
        NavigatorUtils.goLogin(context);
        return false;
      }
      PostBean listItem = manger.getListItem(index);
      if(!listItem.isLiked){
        var likeBean = await PostApi.builder().like(listItem.id);
        manger.notifyDataSetChange((){
          listItem.likes = listItem.likes + 1;
          listItem.isLiked = true;
        });
        if(likeBean != null && likeBean.match){
          // ToastUtils.success("加为好友");
          HeartDialog.show(
            context,
            text: '互赞加为好友',
          );
        }

      }else{
        await PostApi.builder().unLike(listItem.id);
        manger.notifyDataSetChange((){
          listItem.likes = listItem.likes - 1;
          listItem.isLiked = false;
        });
      }
      return !isLike;
    }, onCommentPressed: (){
      if(!isLogin){
        NavigatorUtils.goLogin(context);
        return false;
      }
    }, onCollectPressed: (){
      if(!isLogin){
        NavigatorUtils.goLogin(context);
        return false;
      }
      PostBean listItem = manger.getListItem(index);
      if(!listItem.isCollect){
        PostApi.builder().collect(listItem.id);
        manger.notifyDataSetChange((){
          listItem.isCollect = true;
        });
      }else{
        PostApi.builder().unCollect(listItem.id);
        manger.notifyDataSetChange((){
          listItem.isCollect = false;
        });
      }
    },);
  }

  @override
  List<Widget> renderHeader(BuildContext context, bool innerBoxIsScrolled) {
    List<Widget> widgets = [];

    return widgets;
  }

}
